<!--
 * @Description: 
 * @Autor: dongjunhua
 * @Date: 2021-01-20 09:06:24
 * @LastEditors: dongjunhua
 * @LastEditTime: 2021-02-11 15:06:27
-->

<template>
  <div></div>
</template>

<script>
import { reactive, toRefs } from 'vue';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';
import AuthenticationToken from '@/core/model/AuthenticationToken';
export default {
  name: 'AuthRedirect',
  setup() {
    const route = useRoute();
    const store = useStore();
    const data = reactive({
      ssoAddress: route.query.ssoAddress,
      ssoLogout: route.query.ssoLogout,
      code: route.query.code
    });

    const login = async () => {
      await store.dispatch(
        'subject/login',
        new AuthenticationToken('admin', '123456')
      );
      store.dispatch('subject/permit', { authorizationToken: '1' });
    };

    const goSso = () => {
      const redirect_uri = window.location.href.split('?')[0];
      window.location.href = `${
        data.ssoAddress
      }?redirect_uri=${encodeURIComponent(
        redirect_uri
      )}&response_type=code&scope=1&state=1${
        data.ssoLogout === 'true' ? 'sso_logout=true' : ''
      }`;
    };

    if (data.code) {
      login();
    } else if (data.ssoAddress) {
      goSso();
    }

    return { ...toRefs(data) };
  }
};
</script>
